<?php require 'lib/session_verify.php' ?>
<?php require 'lib/examed_verify.php' ?>
<?php
	require 'lib/student_objective_anwser.php';
	require 'lib/student_true_false_anwser.php';
	require 'lib/student_subjective_anwser.php';

	session_start();

	define('NUM_OBJ_ITEM', 20);
	define('NUM_TF_ITEM', 20);
	define('NUM_SUB_ITEM', 2);

	define('SCORE_SELECTION', 40);
	define('SCORE_TRUE_FALSE', 40);
	define('SCORE_PER_SEL', SCORE_SELECTION / NUM_OBJ_ITEM);
	define('SCORE_PER_TF', SCORE_TRUE_FALSE / NUM_TF_ITEM);

	require 'lib/mysql.php';

	$stmt_cal_score = mysqli_stmt_init($mysqli);
	if(!$stmt_cal_score) {
		die('Statement Initialization Fail, Error: (' . mysqli_errno($mysqli) . ')' . mysqli_error($mysqli)); 
	}

	mysqli_autocommit($mysqli, false);

	/* 记录选择题答题情况, 并计算学生选择题分数 */
	$origin = 'insert into student_objective_anwser(sid, objitemid, stu_selection) values(?, ?, ?)';
	$origin_cal_score  = 'select right_option from objective_item where objitemid=?';
	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_prepare($stmt_cal_score, $origin_cal_score)) {
		die('Objective Caculate ScoreStatemtn Execution Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
	}

	if(!mysqli_stmt_bind_param($mysqli_stmt, 'sis', $sid, $objitemid, $stu_sel)) {
		die('Statement Parameter Binding Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_bind_param($stmt_cal_score, 's', $objitemid)) {
		die('Objective Caculate Score Statemtn Parameter Binding Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
	}

	$sid = $_SESSION['sid'];
	$score_sel = SCORE_SELECTION ;
	for($i = 1 ; $i < NUM_OBJ_ITEM + 1; ++$i) {
		$itemno = 'objitem' . $i;
		$soa = $_SESSION["$itemno"];
		$objitemid = $soa->objitemid;
		$stu_sel = $soa->stuselect;

		if(!mysqli_stmt_execute($mysqli_stmt)) {
			mysqli_rollback($mysqli);
			die('Objective Statemtn Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
		}
		if(!mysqli_stmt_execute($stmt_cal_score)) {
			mysqli_rollback($mysqli);
			die('Objective Caculate ScoreStatemtn Execution Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		if(1 != mysqli_stmt_affected_rows($mysqli_stmt)) {
			mysqli_rollback($mysqli);
			die('Statement Wrong Affected Rows, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
		}

		if(!mysqli_stmt_bind_result($stmt_cal_score, $right_option)) {
			mysqli_rollback($mysqli);
			die('Objective Caculate Score Result Binding Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		if(!mysqli_stmt_store_result($stmt_cal_score)) {
			mysqli_rollback($mysqli);
			die('Objective Caculate Score Storing Result Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		if(1 != mysqli_stmt_num_rows($stmt_cal_score)) {
			mysqli_rollback($mysqli);
			die('Objective Caculate Score Number of Rows Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		mysqli_stmt_fetch($stmt_cal_score);

		if($stu_sel != $right_option) {
			$score_sel -= SCORE_PER_SEL;
		}
		mysqli_stmt_free_result($stmt_cal_score);
	}

	/* 记录判断题答题情况, 并计算判断题分数  */
	$origin = 'insert into student_true_false_anwser(sid, tfitemid, stu_selection) values(?, ?, ?)';
	$origin_cal_score = 'select right_anwser from true_false_item where tfitemid=?';
	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_prepare($stmt_cal_score, $origin_cal_score)) {
		die('Objective Caculate Score Statemtn Preparation Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
	}

	if(!mysqli_stmt_bind_param($mysqli_stmt, 'sis', $sid, $tfitemid, $stu_sel)) {
		die('Statement Parameter Binding Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_bind_param($stmt_cal_score, 's', $tfitemid)) {
		die('Objective Caculate Score Statemtn Parameter Binding Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
	}

	$score_tf = SCORE_TRUE_FALSE;
	for($i = 1 ; $i < NUM_TF_ITEM + 1; ++$i) {
		$itemno = 'tfitem' . $i;
		$stfa = $_SESSION["$itemno"];
		$tfitemid = $stfa->tfitemid;
		$stu_sel = $stfa->stuselect;

		if(!mysqli_stmt_execute($mysqli_stmt)) {
			mysqli_rollback($mysqli);
			die('TF Statemtn Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
		}
		if(1 != mysqli_stmt_affected_rows($mysqli_stmt)) {
			mysqli_rollback($mysqli);
			die('Statement Wrong Affected Rows, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
		}

		if(!mysqli_stmt_execute($stmt_cal_score)) {
			mysqli_rollback($mysqli);
			die('Objective Caculate Score Statemtn Execution Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		if(!mysqli_stmt_bind_result($stmt_cal_score, $right_anwser)) {
			mysqli_rollback($mysqli);
			die('TF Caculate Score Result Binding Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		if(!mysqli_stmt_store_result($stmt_cal_score)) {
			mysqli_rollback($mysqli);
			die('TF Caculate Score Storing Result Fail, Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		if(1 != mysqli_stmt_num_rows($stmt_cal_score)) {
			mysqli_rollback($mysqli);
			die('TF Caculate Score Number of Rows Error: (' . mysqli_stmt_errno($stmt_cal_score) . ')' . mysqli_stmt_error($stmt_cal_score));
		}
		mysqli_stmt_fetch($stmt_cal_score);

		if($stu_sel != $right_anwser) {
			$score_tf -= SCORE_PER_TF;
		}
		mysqli_stmt_free_result($stmt_cal_score);
	}

	/* 记录主观题答题情况 */
	$origin = 'insert into student_subjective_anwser(sid, subitemid, stu_anwser) values(?, ?, ?)';
	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		mysqli_rollback($mysqli);
		die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_bind_param($mysqli_stmt, 'sis', $sid, $subitemid, $stu_anwser)) {
		mysqli_rollback($mysqli);
		die('Statement Parameter Binding Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	for($i = 1 ; $i < NUM_SUB_ITEM + 1; ++$i) {
		$itemno = 'subitem' . $i;
		$subitemid = $_SESSION["$itemno"]->subitemid;
		$stu_anwser = $_SESSION["$itemno"]->text;

		if(!mysqli_stmt_execute($mysqli_stmt)) {
			mysqli_rollback($mysqli);
			die('Subjective Statemtn Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
		}
		if(1 != mysqli_stmt_affected_rows($mysqli_stmt)) {
			mysqli_rollback($mysqli);
			die('Subjective Statement Wrong Affected Rows, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
		}
	}

	/*
	/* 计算学生客观题分数 
	$origin = 'select right_option from objective_item where objitemid=?';
	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		die('Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_bind_param($mysqli_stmt, 
	 */

	/* 标记该学生已考过试, 并记录学生的成绩 */
	$origin = 'insert into score_exam (sid, examed, score_sel, score_tf) values (?, b\'1\', ?, ?)';
	if(!mysqli_stmt_prepare($mysqli_stmt, $origin)) {
		mysqli_rollback($mysqli);
		die('Update Statement Preparation Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_bind_param($mysqli_stmt, 'sii', $sid, $score_sel, $score_tf)) {
		mysqli_rollback($mysqli);
		die('Update Statement Parameter Binding Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(!mysqli_stmt_execute($mysqli_stmt)) {
		mysqli_rollback($mysqli);
		die('Update Statemtn Execution Fail, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}
	if(1 != mysqli_stmt_affected_rows($mysqli_stmt)) {
		mysqli_rollback($mysqli);
		die('Update Statement Wrong Affected Rows, Error: (' . mysqli_stmt_errno($mysqli_stmt) . ')' . mysqli_stmt_error($mysqli_stmt));
	}

	mysqli_commit($mysqli);

	session_write_close();

	mysqli_stmt_close($mysqli_stmt);
	mysqli_close($mysqli);

	session_start();
	$_SESSION['examed'] = 1;
	session_write_close();

	header("Location: thanks.php");
?>
